<?php

namespace addons\yunfood\model;

use addons\yunfood\library\ModelTrait;
use app\common\model\User;
use think\Model;
use traits\model\SoftDelete;

/**
 * 订单 - 发票模型
 */
class OrderInvoice extends Model
{
    use ModelTrait;
    use SoftDelete;

    // 表名,不含前缀
    protected $name = 'yunfood_order_invoice';

    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 定义时间戳字段名
    protected $deleteTime = 'deleted_at';
    protected $updateTime = 'updated_at';
    protected $createTime = 'created_at';

    // 不显示的字段
    protected $hidden = ['deleted_at'];

    protected $resultSetType = 'collection';

    public function scopeShow($query)
    {
        return $query->where('status', '=', self::STATUS_WAIT);
    }

    // 用户数据
    public function userData()
    {
        return $this->hasOne(User::class, 'id', 'user_id');
    }

    // 订单数据
    public function orderData()
    {
        return $this->hasOne(Order::class, 'order_no', 'order_no');
    }

    // 类别
    const TYPE_NORMAL = 'normal';

    public static function getTypeList()
    {
        return [self::TYPE_NORMAL => __('普通发票')];
    }

    public function getTypeTextAttr($value, $data)
    {
        $optionList = self::getTypeList();

        return $optionList[$data['type']] ?? $data['type'];
    }

    // 抬头类别
    const TITLE_TYPE_COMPANY = 'company';
    const TITLE_TYPE_OTHER   = 'other';

    public static function getTitleTypeList()
    {
        return [self::TITLE_TYPE_COMPANY => __('企业'), self::TITLE_TYPE_OTHER => __('个人/组织')];
    }

    public function getTitleTypeTextAttr($value, $data)
    {
        $optionList = self::getTitleTypeList();

        return $optionList[$data['title_type']] ?? $data['title_type'];
    }

    // 开票图片列表
    public function getInvoicePhotoListAttr($value, $data)
    {
        $photoList = [];
        if ($photos = $data['invoice_photos']) {
            $photos = explode(',', $photos);
            foreach ($photos as $item) {
                $photoList[] = ['src' => $item, 'url' => cdnurl($item, true)];
            }
        }

        return $photoList;
    }

    // 开票文件列表
    public function getInvoiceFileListAttr($value, $data)
    {
        $fileList = [];
        if ($files = $data['invoice_files']) {
            $files = explode(',', $files);
            foreach ($files as $item) {
                $fileList[] = ['src' => $item, 'url' => cdnurl($item, true)];
            }
        }

        return $fileList;
    }

    // 状态
    const STATUS_WAIT   = 1;
    const STATUS_DONE   = 2;
    const STATUS_CANCEL = 3;
    const STATUS_REFUSE = 4;

    public static function getStatusList()
    {
        return [
            self::STATUS_WAIT   => __('待处理'),
            self::STATUS_DONE   => __('已开票'),
            self::STATUS_CANCEL => __('已取消'),
            self::STATUS_REFUSE => __('已拒绝')
        ];
    }

    public function getStatusTextAttr($value, $data)
    {
        $optionList = self::getStatusList();

        return $optionList[$data['status']] ?? $data['status'];
    }
}
